package com.jjyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jjyu.entity.FavoriteRestaurant;
import com.jjyu.service.FavoriteRestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/favorite-restaurant")
public class FavoriteRestaurantController {

    @Autowired
    private FavoriteRestaurantService favoriteRestaurantService;

    // 新增商品收藏
    @PostMapping("/add")
    public ResponseEntity<?> addFavoriteRestaurant(@RequestBody FavoriteRestaurant favoriteRestaurant) {
        boolean result = favoriteRestaurantService.addFavoriteRestaurant(favoriteRestaurant);
        if (result) {
            return ResponseEntity.ok("Favorite restaurant added successfully.");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add favorite restaurant.");
        }
    }

    // 删除商品收藏
    @DeleteMapping("/delete/{favoriteId}")
    public ResponseEntity<?> deleteFavoriteRestaurant(@PathVariable Integer favoriteId) {
        boolean result = favoriteRestaurantService.deleteFavoriteRestaurant(favoriteId);
        if (result) {
            return ResponseEntity.ok("Favorite restaurant deleted successfully.");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to delete favorite restaurant.");
        }
    }

    // 修改商品收藏
    @PutMapping("/update")
    public ResponseEntity<?> updateFavoriteRestaurant(@RequestBody FavoriteRestaurant favoriteRestaurant) {
        boolean result = favoriteRestaurantService.updateFavoriteRestaurant(favoriteRestaurant);
        if (result) {
            return ResponseEntity.ok("Favorite restaurant updated successfully.");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to update favorite restaurant.");
        }
    }

    // 根据用户ID查找商品收藏
    @GetMapping("/user/{userId}")
    public ResponseEntity<?> findFavoriteRestaurantsByUserId(@PathVariable Integer userId) {
        List<FavoriteRestaurant> favoriteRestaurants = favoriteRestaurantService.findFavoriteRestaurantsByUserId(userId);
        if (favoriteRestaurants.isEmpty()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No favorite restaurants found for the user.");
        } else {
            return ResponseEntity.ok(favoriteRestaurants);
        }
    }
}

